package com.guigu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.guigu.BusinessesService;
import com.guigu.ZhucheService;
import com.guigu.pojo.Businesses;
import com.guigu.pojo.FrontUser;
import com.guigu.pojo.FrontUser2;
import com.guigu.vo.BusinessesVo;
import com.guigu.vo.Pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@CrossOrigin
@RestController
@RequestMapping("Businesses")
public class BusinessesController {
    @Autowired
    private BusinessesService businessesService;
    @Autowired
    private ZhucheService zhucheService;
    @RequestMapping("selectAll/{pageNo}/{pageSize}")
    public Object selectAll(BusinessesVo vo, @PathVariable int pageNo,@PathVariable int pageSize) {
        PageInfo<Businesses> businessesPageInfo = businessesService.selectAll(vo, pageNo, pageSize);
        Pojo<Businesses> objectPojo = new Pojo<>();
        objectPojo.setRows(businessesPageInfo.getList());
        objectPojo.setTotal(businessesPageInfo.getTotal());
        return objectPojo;
    }
    //查询用户下的商户(已审核)
    @PostMapping("{id}")
    public Businesses One(@PathVariable int id) {
        Businesses one = businessesService.One(id);
        return one;
    }

    @PutMapping("update")
    public String update(@RequestBody Businesses businesses) {
        boolean b = businessesService.updateById(businesses);
        if (b)
            return "ok";
        return "no";
    }
    @PostMapping("insert")
    public String insert(@RequestBody Businesses businesses) {
        businesses.setCheckTag("S001-0");
        businesses.setIsBusiness(0);
        boolean b = businessesService.save(businesses);
        if (b) {
            int userId = businesses.getUserId();
            FrontUser user = zhucheService.One(userId);
            user.setIsBusinesses(0);
            boolean b1 = zhucheService.updateById(user);
            if (b1)
                return "ok";
            return "no";
        }
        return "no";
    }
    //xiaosong
    //分页查询
    @RequestMapping("/select")
    public Pojo<FrontUser2> getPage(int pageNum, int pageSize, @RequestBody BusinessesVo vo)
    {
        System.out.println("vo"+vo);
        System.out.println("pageNum"+pageNum);
        System.out.println("pageSize"+pageSize);
        Pojo<FrontUser2> page = businessesService.getPage(pageNum, pageSize, vo);
        System.out.println("page"+page);
        return page;
    }
    //根据id查询
    @RequestMapping("/selectById")
    public Businesses selectById(int id) {
        System.out.println("businesses"+id);
//        QueryWrapper<Businesses> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_id",userId);
        Businesses businesses = businessesService.getById(id);
        System.out.println("busine"+businesses);
        return businesses;
    }
    //根据id修改
    @PutMapping("/updateById")
    public String updateById(@RequestBody Businesses businesses) {
        System.out.println("businesses"+businesses);
        System.out.println("999"+businesses);
        //将String类型转换为int类型
        boolean b = businessesService.updateById(businesses);
        if (b)
            return "ok";
        return "no";
    }
    //根据userId删除
    @DeleteMapping("/deleteById/{userId}")
    public String deleteById(@PathVariable int userId) {
        System.out.println("businesses"+userId);
        QueryWrapper<Businesses> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        boolean b = businessesService.remove(queryWrapper);
        if (b)
            return "ok";
        return "no";
    }
    //根据userId查询
    @RequestMapping("/selectById1/{userId}")
    public Businesses selectById1(@PathVariable int userId) {
        System.out.println("businesses"+userId);
        QueryWrapper<Businesses> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        Businesses businesses = businessesService.getOne(queryWrapper);
        System.out.println("businesses"+businesses);
        return businesses;
    }
}

